package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);

    Page<Orders> page(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select * from orders where id=#{id}")
    Orders getById(Long id);

    void update(Orders orders);

    @Select("select count(id) from orders where status=#{status}")
    Integer getCountByStatus(Integer status);

    @Select("select id from orders where status=#{status} and order_time<=#{localDateTime}")
    List<Long> getTimeoutIds(Integer status, LocalDateTime localDateTime);

    void updateStatusBatch(List<Long> ids);

    @Update("update orders set status=5 where status=4")
    void updateDeliveryBatch();

    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    @Select("SELECT SUM(amount) as turnover FROM `orders` " +
            "where status=5 and order_time>=#{begin} and order_time<=#{end}" +
            "group by date(order_time) order by order_time asc")
    List<BigDecimal> turnoverStatistics(LocalDate begin, LocalDate end);

    @Select("SELECT SUM(amount) as turnover FROM `orders` where status=5 and order_time=#{begin}")
    Double turnover(LocalDate begin);

    @Select("SELECT date(order_time) as date FROM `orders`" +
            "where status=5 and order_time>=#{begin} and order_time<=#{end}" +
            "group by date(order_time)  order by order_time asc")
    List<LocalDate> datesToAmounts(LocalDate begin, LocalDate end);

    @Select("SELECT COUNT(id) FROM `user` where DATE(create_time)<=#{begin}")
    Integer totalUser(LocalDate begin);

    @Select("SELECT COUNT(id) FROM `user` where DATE(create_time)=#{begin}")
    Integer newUser(LocalDate begin);

    @Select("SELECT COUNT(id) FROM `orders` where DATE(order_time)=#{begin}")
    Integer orderCount(LocalDate begin);

    @Select("SELECT COUNT(id) FROM `orders` where DATE(order_time)=#{begin} and `status`=5")
    Integer validOrder(LocalDate begin);

    @Select("SELECT COUNT(id) FROM `orders` where DATE(order_time)<=#{end}")
    Integer totalOrderCount(LocalDate end);

    @Select("SELECT COUNT(id) FROM `orders` where DATE(order_time)<=#{end} and `status`=5")
    Integer validOrderCount(LocalDate end);

    @Select("SELECT name,SUM(number) as number FROM `order_detail` where order_id in " +
            "(select id from orders where DATE(order_time)>=#{begin} and DATE(order_time)<=#{end}) " +
            "GROUP BY `name` order by number DESC LIMIT 10")
    List<GoodsSalesDTO> salesTop10(LocalDate begin, LocalDate end);


    Integer countByMap(Map map);
}
